var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var conn = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '233',
    database:'blog',
    port: 3306
});
conn.connect();
/* GET home page. */
router.get('/', function(req, res, next) {
	conn.query('select * from catalog', function(err, rows, fields) {
		if (err) throw err;
		catalog = rows;
		conn.query('select *,articles.id as aid from articles join catalog on articles.cata_id=catalog.id limit 10', function(err, rows, fields) {
			if (err) throw err;
			item = rows;
	  			res.render('index', { title: '机友汇(jiyouh.com)' ,item:item,cata_id:0,catalog:catalog});
		})
	})
});
router.get('/news', function(req, res, next) {
	conn.query('select * from catalog', function(err, rows, fields) {
		if (err) throw err;
		catalog = rows;
		conn.query('select * from articles where cata_id = '+req.query.id+' limit 10', function(err, rows, fields) {
			if (err) throw err;
			item = rows;
		  		res.render('list', { title: '机友汇(jiyouh.com)' ,item:item,cata_id:req.query.id,catalog:catalog});
		})
	})
});
router.get('/article', function(req, res, next) {
	conn.query('select * from catalog', function(err, rows, fields) {
		if (err) throw err;
		catalog = rows;
		conn.query('select * from articles where id = '+req.query.id, function(err, rows, fields) {
			if (err) throw err;
			item = rows;
		  		res.render('article', { title: '机友汇(jiyouh.com)' ,article:rows[0],catalog:catalog,cata_id:rows[0].cata_id});
		})
	})																				
});

module.exports = router;
